package com.jackson.passjava.question.controller;

import java.util.Arrays;
import java.util.Map;

import com.jackson.passjava.question.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jackson.passjava.question.entity.QuestionEntity;
import com.jackson.passjava.common.utils.PageUtils;
import com.jackson.passjava.common.utils.R;

import javax.validation.Valid;


/**
 * 
 *
 * @author Liyuhang
 * @email 50875895@qq.com
 * @date 2021-08-15 23:06:00
 */
@RestController
@RequestMapping("passjavaquestion/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = questionService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) throws Exception {
        if(id<0L){
            //测试捕捉全局异常
            throw new Exception("id不合法");
        }
		QuestionEntity question = questionService.getById(id);
        return R.ok().put("question", question);
    }

    /**
     * http://localhost:8001/passjavaquestion/question/save
     * {
     *     "displayOrder": 0.2
     * }
     * result
     * {
     *     "timestamp": "2021-08-17T16:53:56.714+0000",
     *     "status": 400,
     *     "error": "Bad Request",
     *     "errors": [
     *         {
     *             "codes": [
     *                 "Positive.questionEntity.displayOrder",
     *                 "Positive.displayOrder",
     *                 "Positive.java.lang.Integer",
     *                 "Positive"
     *             ],
     *             "arguments": [
     *                 {
     *                     "codes": [
     *                         "questionEntity.displayOrder",
     *                         "displayOrder"
     *                     ],
     *                     "arguments": null,
     *                     "defaultMessage": "displayOrder",
     *                     "code": "displayOrder"
     *                 }
     *             ],
     *             "defaultMessage": "必须是正数",
     *             "objectName": "questionEntity",
     *             "field": "displayOrder",
     *             "rejectedValue": 0,
     *             "bindingFailure": false,
     *             "code": "Positive"
     *         }
     *     ],
     *     "message": "Validation failed for object='questionEntity'. Error count: 1",
     *     "path": "/passjavaquestion/question/save"
     * }
     *
     */
    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Valid @RequestBody QuestionEntity question){
		questionService.save(question);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody QuestionEntity question){
		questionService.updateById(question);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		questionService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
